package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MenuDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Menu;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户服务接口实现类
 * @author wangxin
 * @version 1.0
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService
{

    @Autowired
    private UserDao userDao;

    @Autowired
    private MenuDao menuDao;

    /**
     * 根据用户名查询数据用户信息
     * @param username
     * @return
     */
    @Override
    public User findByUserName(String username) {
        return userDao.findByUserName(username);
    }


    //根据用户名获取菜单
    @Override
    public List<Menu> getmenus(String username) {
        List<Menu> resultList=new ArrayList<>();

        List<Menu> childrenList=new ArrayList<>();
        //根据用户名菜单
          List<Menu> menuList=menuDao.getmenus(username);
          if (menuList!=null && menuList.size()>0){
              for (Menu menu : menuList) {
                  if (menu.getParentMenuId()==null){
                      resultList.add(menu); //主菜单
                  }else {
                      childrenList.add(menu); //子菜单
                  }
              }
          }

        for (Menu menu : resultList) {
            List<Menu> childrens=new ArrayList<>();
            for (Menu children : childrenList) {
                if (menu.getId()==children.getParentMenuId()){
                    childrens.add(children);
                }
            }
            menu.setChildren(childrens);
        }


        return resultList;
    }

    @Override
    public User findById(Integer userId) {
        return userDao.findById(userId);
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        PageHelper.startPage(currentPage,pageSize);

        Page<User> userPage = userDao.selectByCondition(queryString);

        return new PageResult(userPage.getTotal(),userPage.getResult());
    }


    /**
     * 新增用户
     * @param user
     * @param roleIds
     */
    @Override
    public void add(User user, Integer[] roleIds) {
        userDao.add(user);

        setUserAndRole(user.getId(),roleIds);
    }


    /**
     * 删除用户
     * @param userId
     */
    @Override
    public void deleteById(Integer userId) {
        int count = userDao.findUserAndRoleCountByUserId(userId);
        if (count > 0){
            throw new RuntimeException(MessageConstant.DELETE_USER_ROLE_FAIL);
        }
        userDao.deleteById(userId);
    }

    @Override
    public List<Integer> findRoleIdsByUserId(Integer userId) {
        return userDao.findRoleIdsByUserId(userId);
    }

    @Override
    public void edit(User user, Integer[] roleIds) {
        userDao.edit(user);

        userDao.deleteAssociation(user.getId());

        setUserAndRole(user.getId(),roleIds);
    }

    /**
     * 中间表插入记录
     *
     * @param userId
     * @param roleIds
     */
    private void setUserAndRole(Integer userId,Integer[] roleIds){
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("userId",userId);
                map.put("roleId",roleId);
                userDao.setUserAndRole(map);
            }
        }
    }
}
